package com.yinkgh.mybatis.base.page;

/**
 * 类名称: Page 类描述: oracle,sqlserver,mysql分页技术实现 创建人: wangshiping 创建时间:
 * 2015年10月30日 上午9:15:23 修改人: 修改时间: 修改备注:
 * 
 * @version v1.0.0
 */
public class Page {

	// 数据库配置文件名称
	private static final String SYSTEMDATEBASETYPE = Configuration.getInstance().getConfiguration("DataBaseType"); // MYSQL
																													// ORACLE

	private int pageId = 1; // 当前页
	private int rowCount = 0; // 总行数
	private int pageSize = 100; // 页大小
	private int pageCount = 0; // 总页数
	private int pageOffset = 0;// 当前页起始记录
	private int pageTail = 0;// 当前页到达的记录
	private String orderField;
	private String orderDirection;

	private boolean paging = false; // 默认分页

	// 页面显示分页按钮个数
	private int length = 6;
	// 开始分页数字
	private int startIndex = 0;
	// 结束分页数字
	private int endIndex = 0;

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getStartIndex() {
		startIndex = pageId - (length / 2);
		if (startIndex < 1) {
			startIndex = 1;
		}
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getEndIndex() {
		if (getStartIndex() < 1) {
			setStartIndex(1);
		}
		endIndex = (getStartIndex() + length) <= getPageCount() ? (getStartIndex() + length) : getPageCount();
		return endIndex;
	}

	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	public Page() {
	}

	protected void doPage() {
		this.pageCount = this.rowCount / this.pageSize + 1;
		// 如果模板==0，且总数大于1，则减一
		if ((this.rowCount % this.pageSize == 0) && pageCount > 1)
			this.pageCount--;

		// Mysql 算法
		this.pageOffset = (this.pageId - 1) * this.pageSize;
		this.pageTail = this.pageOffset + this.pageSize;
		if ((this.pageOffset + this.pageSize) > this.rowCount) {
			this.pageTail = this.rowCount;
		}
	}

	public String getOrderCondition() {
		String condition = "";
		if (this.orderField != null && this.orderField.length() != 0) {
			condition = " order by " + orderField + " " + orderDirection + " ";
		}

		return condition;
	}

	public String getMysqlQueryCondition() {
		String condition = "";
		if (paging) {
			condition = " limit " + pageOffset + "," + pageSize;
		}
		return condition;
	}

	public String getStartQueryCondition() {
		String condition = "";
		if (paging) {
			condition = "select * from ( select ROW_.*, ROWNUM ROW_NUM from ("; // 默认分页
			if (SYSTEMDATEBASETYPE != null) {
				if (SYSTEMDATEBASETYPE.equals("ORACLE")) {
					condition = "select * from ( select ROW_.*, ROWNUM ROW_NUM from (";
				}
			}
		}
		return condition;
	}

	public String getEndQueryCondition() {
		String condition = "";
		if (paging) {
			if (SYSTEMDATEBASETYPE != null) {
				if (SYSTEMDATEBASETYPE.equals("ORACLE")) {
					condition = " ) ROW_  where rownum <=  " + pageTail + ") where ROW_NUM > " + pageOffset;
				} else if (SYSTEMDATEBASETYPE.equals("MYSQL")) {
					condition = " limit " + pageOffset + "," + pageSize;
				}
			} else { // 默认oracle方式分页
				condition = " ) ROW_  where rownum <=  " + pageTail + ") where ROW_NUM > " + pageOffset;
			}
		}
		return condition;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String isOrderDirection() {
		return orderDirection;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageId(int pageId) {
		this.pageId = pageId;
	}

	public int getPageId() {
		return pageId;
	}

	public void setPageOffset(int pageOffset) {
		this.pageOffset = pageOffset;
	}

	public int getPageOffset() {
		return pageOffset;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageTail(int pageTail) {
		this.pageTail = pageTail;
	}

	public int getPageTail() {
		return pageTail;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
		this.doPage();
	}

	public int getRowCount() {
		return rowCount;
	}

	public boolean getPaging() {
		return paging;
	}

	public void setPaging(boolean paging) {
		this.paging = paging;
	}

}